//
//  XTLoveHeaerView.m
//  XTAnimations
//
//  Created by zjwang on 16/7/14.
//  Copyright © 2016年 夏天然后. All rights reserved.
//

#import "XTLoveHeartView.h"

@interface XTLoveHeartView ()

@property (nonatomic, strong) UIColor *strokeColor;
@property (nonatomic, strong) UIColor *fillColor;


@end

@implementation XTLoveHeartView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _strokeColor = [UIColor whiteColor];
        _fillColor  = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];
        
        self.backgroundColor   = [UIColor clearColor];
        self.layer.anchorPoint = CGPointMake(0.5, 1);

    }
    return self;
}


static CGFloat animateTime = 0.2;



- (void)animateInView:(UIView *)view andfinishBlock:(void (^)())finishBlock
{
    NSTimeInterval totalAnimationDuration = 8 * 0.5;
    CGFloat heartSize = CGRectGetWidth(self.bounds);
    CGFloat heartCenterX = self.center.x;
    CGFloat viewHeight = CGRectGetHeight(view.bounds);
    CGFloat viewWidth = CGRectGetWidth(view.bounds);
    
    //Pre-Animation setup
    self.transform = CGAffineTransformMakeScale(0, 0);
    self.alpha = 0;
    
    //Bloom
    [UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.6 initialSpringVelocity:0.8 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.transform = CGAffineTransformIdentity;
        self.alpha = 0.9;
    } completion:NULL];
    
    NSInteger i = arc4random_uniform(2);
    // -1 OR 1
    NSInteger rotationDirection = 1 - (2 * i);
    NSInteger rotationFraction = arc4random_uniform(10);
    [UIView animateWithDuration:totalAnimationDuration animations:^{
        self.transform = CGAffineTransformMakeRotation(rotationDirection * M_PI / (16 + rotationFraction * 0.2));
    } completion:NULL];
    
    UIBezierPath *heartTravelPath = [UIBezierPath bezierPath];
    [heartTravelPath moveToPoint:self.center];
    //    [heartTravelPath moveToPoint:CGPointMake(self.center.x, 100)];
    
    //random end point
    //    CGPoint endPoint = CGPointMake(heartCenterX + (rotationDirection) * arc4random_uniform(2 * heartSize), viewHeight/6.0 + arc4random_uniform(viewHeight / 4.0));
    
    // 随机到的终点
    //    CGPoint endPoint = CGPointMake(heartCenterX + (rotationDirection) * arc4random_uniform(2 * heartSize), viewHeight * 0.5 + arc4random_uniform(viewHeight * 0.5));
    
    CGPoint endPoint = CGPointMake(viewWidth * 0.7 + arc4random_uniform(viewWidth * 0.25)  , 0);
    
    //random Control Points
    NSInteger j = arc4random_uniform(2);
    NSInteger travelDirection = 1- (2*j);// -1 OR 1
    
    //randomize x and y for control points
    CGFloat xDelta = (heartSize/2.0 + arc4random_uniform(2*heartSize)) * travelDirection;
    //    CGFloat yDelta = MAX(endPoint.y ,MAX(arc4random_uniform(8*heartSize), heartSize));
    
    CGFloat yDelta = MAX(endPoint.y , heartSize);
    
    CGPoint controlPoint1 = CGPointMake(heartCenterX + xDelta, viewHeight - yDelta + arc4random_uniform(viewHeight * 0.4));
    CGPoint controlPoint2 = CGPointMake(heartCenterX - xDelta, yDelta + arc4random_uniform(viewHeight * 0.25));
    
    [heartTravelPath addCurveToPoint:endPoint controlPoint1:controlPoint1 controlPoint2:controlPoint2];
    
    //    [heartTravelPath addCurveToPoint:controlPoint1 controlPoint1:controlPoint2 controlPoint2:endPoint];
    
    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyFrameAnimation.path = heartTravelPath.CGPath;
    keyFrameAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
//    keyFrameAnimation.duration = totalAnimationDuration + endPoint.y/viewHeight;
    keyFrameAnimation.duration = totalAnimationDuration + endPoint.y/viewHeight / 2;
    [self.layer addAnimation:keyFrameAnimation forKey:@"positionOnPath"];
    
    // Alpha & remove from superview
    [UIView animateWithDuration:totalAnimationDuration animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        finishBlock();
    }];
    
}






//- (void)animateInView:(UIView *)view
//{
//    NSTimeInterval totalAnimationDuration = 8;
//    CGFloat heartSize = CGRectGetWidth(self.bounds);
//    CGFloat heartCenterX = self.center.x;
//    CGFloat viewHeight = CGRectGetHeight(view.bounds);
//    CGFloat viewWidth = CGRectGetWidth(view.bounds);
//    
//    //Pre-Animation setup
//    self.transform = CGAffineTransformMakeScale(0, 0);
//    self.alpha = 0;
//    
//    //Bloom
//    [UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.6 initialSpringVelocity:0.8 options:UIViewAnimationOptionCurveEaseOut animations:^{
//        self.transform = CGAffineTransformIdentity;
//        self.alpha = 0.9;
//    } completion:NULL];
//    
//    NSInteger i = arc4random_uniform(2);
//    // -1 OR 1
//    NSInteger rotationDirection = 1 - (2 * i);
//    NSInteger rotationFraction = arc4random_uniform(10);
//    [UIView animateWithDuration:totalAnimationDuration animations:^{
//        self.transform = CGAffineTransformMakeRotation(rotationDirection * M_PI / (16 + rotationFraction * 0.2));
//    } completion:NULL];
//    
//    UIBezierPath *heartTravelPath = [UIBezierPath bezierPath];
//    [heartTravelPath moveToPoint:self.center];
////    [heartTravelPath moveToPoint:CGPointMake(self.center.x, 100)];
//    
//    //random end point
////    CGPoint endPoint = CGPointMake(heartCenterX + (rotationDirection) * arc4random_uniform(2 * heartSize), viewHeight/6.0 + arc4random_uniform(viewHeight / 4.0));
//
//    // 随机到的终点
////    CGPoint endPoint = CGPointMake(heartCenterX + (rotationDirection) * arc4random_uniform(2 * heartSize), viewHeight * 0.5 + arc4random_uniform(viewHeight * 0.5));
//    
//    CGPoint endPoint = CGPointMake(viewWidth * 0.7 + arc4random_uniform(viewWidth * 0.25)  , viewHeight * 0.5);
//    
//    //random Control Points
//    NSInteger j = arc4random_uniform(2);
//    NSInteger travelDirection = 1- (2*j);// -1 OR 1
//    
//    //randomize x and y for control points
//    CGFloat xDelta = (heartSize/2.0 + arc4random_uniform(2*heartSize)) * travelDirection;
////    CGFloat yDelta = MAX(endPoint.y ,MAX(arc4random_uniform(8*heartSize), heartSize));
//    
//    CGFloat yDelta = MAX(endPoint.y , heartSize);
//    
//    CGPoint controlPoint1 = CGPointMake(heartCenterX + xDelta, viewHeight - yDelta + arc4random_uniform(viewHeight * 0.35));
//    CGPoint controlPoint2 = CGPointMake(heartCenterX - xDelta, yDelta + arc4random_uniform(viewHeight * 0.15));
//    
//    [heartTravelPath addCurveToPoint:endPoint controlPoint1:controlPoint1 controlPoint2:controlPoint2];
//    
////    [heartTravelPath addCurveToPoint:controlPoint1 controlPoint1:controlPoint2 controlPoint2:endPoint];
//    
//    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//    keyFrameAnimation.path = heartTravelPath.CGPath;
//    keyFrameAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
//    keyFrameAnimation.duration = totalAnimationDuration + endPoint.y/viewHeight;
//    [self.layer addAnimation:keyFrameAnimation forKey:@"positionOnPath"];
//    
//    // Alpha & remove from superview
//    [UIView animateWithDuration:totalAnimationDuration animations:^{
//        self.alpha = 0.0;
//    } completion:^(BOOL finished) {
//        [self removeFromSuperview];
//    }];
//
//}




- (void)drawRect:(CGRect)rect
{
    [_strokeColor setStroke];
    [_fillColor setFill];
    
    CGFloat drawingPadding = 4.0;
    CGFloat curveRadius = floor((CGRectGetWidth(rect) - 2*drawingPadding) / 4.0);
    
    //Creat path
    UIBezierPath *heartPath = [UIBezierPath bezierPath];
    
    //Start at bottom heart tip
    CGPoint tipLocation = CGPointMake(floor(CGRectGetWidth(rect) / 2.0), CGRectGetHeight(rect) - drawingPadding);
    [heartPath moveToPoint:tipLocation];
    
    //Move to top left start of curve
    CGPoint topLeftCurveStart = CGPointMake(drawingPadding, floor(CGRectGetHeight(rect) / 2.4));
    
    [heartPath addQuadCurveToPoint:topLeftCurveStart controlPoint:CGPointMake(topLeftCurveStart.x, topLeftCurveStart.y + curveRadius)];
    
    //Create top left curve
    [heartPath addArcWithCenter:CGPointMake(topLeftCurveStart.x + curveRadius, topLeftCurveStart.y) radius:curveRadius startAngle:M_PI endAngle:0 clockwise:YES];
    
    //Create top right curve
    CGPoint topRightCurveStart = CGPointMake(topLeftCurveStart.x + 2*curveRadius, topLeftCurveStart.y);
    [heartPath addArcWithCenter:CGPointMake(topRightCurveStart.x + curveRadius, topRightCurveStart.y) radius:curveRadius startAngle:M_PI endAngle:0 clockwise:YES];
    
    //Final curve to bottom heart tip
    CGPoint topRightCurveEnd = CGPointMake(topLeftCurveStart.x + 4*curveRadius, topRightCurveStart.y);
    [heartPath addQuadCurveToPoint:tipLocation controlPoint:CGPointMake(topRightCurveEnd.x, topRightCurveEnd.y + curveRadius)];
    
    [heartPath fill];
    heartPath.lineWidth = 1;
    heartPath.lineCapStyle = kCGLineCapRound;
    heartPath.lineJoinStyle = kCGLineCapRound;
    [heartPath stroke];
}
@end
